package com.fhz.mvvm.ui.main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.fhz.mvvm.R;
import com.fhz.mvvm.callback.ApiResponse;
import com.fhz.mvvm.dao.AppDataBase;
import com.fhz.mvvm.databinding.ActivityMainBinding;
import com.fhz.mvvm.entity.Banner;
import com.fhz.mvvm.entity.GoodsInfo;
import com.fhz.mvvm.entity.GoodsInfoLocal;
import com.fhz.mvvm.ui.login.LoginActivity;
import com.fhz.mvvm.util.MyRunnable;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {

    MainViewModel mMainViewModel;
    ActivityMainBinding mBinding;
    ArrayList<GoodsInfo.ListDTO> listDTOS;

    ArrayList<GoodsInfoLocal> mArrayList = new ArrayList<GoodsInfoLocal>();

    @Inject
    ExecutorService mExecutorService;

//    MainRecyclerViewAdapter mainRecyclerViewAdapter;
    MainDataBindingRecyclerViewAdapter mainRecyclerViewAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());


        mMainViewModel = new ViewModelProvider(this).get(MainViewModel.class);

        mBinding.banner.addBannerLifecycleObserver(this);
        listDTOS = new ArrayList<>();

        setRVAdapter();
        requestGoodsInfo();
        mMainViewModel.getBanner(0, 1, 5).observe(this, new Observer<ApiResponse<Banner>>() {
            @Override
            public void onChanged(ApiResponse<Banner> bannerApiResponse) {
//                System.out.println(bannerApiResponse.data.getList().get(0).getImagepath());

                mBinding.banner.setAdapter(new BannerImageAdapter<Banner.ListDTO>(bannerApiResponse.data.getList()) {
                            @Override
                            public void onBindView(BannerImageHolder holder, Banner.ListDTO data, int position, int size) {
                                Glide.with(holder.itemView)
                                        .load(data.getImagepath())
                                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                        .into(holder.imageView);
                            }
                        })
                        .setIndicator(new CircleIndicator(MainActivity.this));

            }
        });

    }



    private void requestGoodsInfo() {
        mMainViewModel.getGoodsInfo(1,10).observe(this, new Observer<ApiResponse<GoodsInfo>>() {
            @Override
            public void onChanged(ApiResponse<GoodsInfo> goodsInfoApiResponse) {
                listDTOS.addAll(goodsInfoApiResponse.getData().getList());

                for (GoodsInfo.ListDTO listDTO : goodsInfoApiResponse.getData().getList()) {
                    GoodsInfoLocal goodsInfoLocal = new GoodsInfoLocal();
                    goodsInfoLocal.setCategoryId(listDTO.getCategoryId());
                    goodsInfoLocal.setGoodsDefaultIcon(listDTO.getGoodsDefaultIcon());
                    goodsInfoLocal.setGoodsDesc(listDTO.getGoodsDesc());
                    mArrayList.add(goodsInfoLocal);
                }

                mainRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
    }

    private void setRVAdapter() {

//        mainRecyclerViewAdapter = new MainRecyclerViewAdapter(this, listDTOS);
        mainRecyclerViewAdapter = new MainDataBindingRecyclerViewAdapter(this, listDTOS);
        mBinding.mainRv.setLayoutManager(new GridLayoutManager(this,2));
        mBinding.mainRv.setAdapter(mainRecyclerViewAdapter);

        mainRecyclerViewAdapter.setOnItemClickListener((v, position) -> {

            //跳转到登录的界面
            LoginActivity.launch(this,"userName");

            Toast.makeText(MainActivity.this, "位置".concat(String.valueOf(position)), Toast.LENGTH_SHORT).show();


            mMainViewModel.getAllGoods().observe(MainActivity.this, new Observer<List<GoodsInfoLocal>>() {
                @Override
                public void onChanged(List<GoodsInfoLocal> goodsInfoLocals) {
                    System.out.println(goodsInfoLocals.size());
                }
            });

            MyRunnable myRunnable = new MyRunnable("userdao") {
                @Override
                protected void execute() {
                    //添加到数据库.
                    mMainViewModel.insertGoodsInfo(mArrayList.get(position));

                }
            };

            mExecutorService.execute(myRunnable);

        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}